package com.engine.update.cmd;

import com.alibaba.fastjson.JSONObject;
import com.engine.core.dao.CodeResourceMapper;
import com.engine.core.domain.GenTable;
import com.engine.core.domain.GenTableColumn;
import com.engine.core.domain.JavaFunction;
import com.engine.core.gen.ParamOption;
import com.engine.core.interceptor.JdbcCommandContext;
import com.engine.core.interceptor.ResponseCommand;
import com.engine.core.rowMapper.JdbcTypeBiz;
import com.engine.core.rowMapper.MappedType;
import com.engine.core.util.JdbcTemplateUtils;
import com.engine.core.util.QueryUtil;
import com.engine.core.util.Response;
import com.engine.core.util.Utils;
import com.engine.system.User;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.*;

public class SaveMultiCommand extends ResponseCommand {

    public SaveMultiCommand(User user, Map<String, Object> params) {
        super(user, params);
    }

    @Override
    public Response execute(JdbcCommandContext commandContext) throws Exception {
        Response response=Response.static_success("执行成功!");
        try {
            boolean isbatch= Utils.nullDefault((String)params.get(QueryUtil.UPDATA_IS_BATCH),"0").equals("1");

            params.put(QueryUtil.QUERY_PARAMS_URLENCODE,"0");
            ParamOption paramOption=new ParamOption(params);
            JSONObject paramRoot=JSONObject.parseObject((String)params.get(QueryUtil.QUERY_CONTENT));
            CodeResourceMapper codeResourceMapper=commandContext.getApplicationContext().getBean(CodeResourceMapper.class);

            JdbcTemplate jdbcTemplate=commandContext.getJdbcTemplate();
            String columns= paramRoot.getString(QueryUtil.QUERY_COLUMNS);

            String resourceId=Utils.urlDecode(paramRoot.getString(QueryUtil.QUERY_RESOURCE),paramOption);
            String[] dateformat=commandContext.getGlobal().getDateformat();
            GenTable genTable=codeResourceMapper.getGenTable(Utils.getInt(resourceId,-1));
            String tableName= genTable.getResourceName();
            String datas=paramRoot.getString(QueryUtil.UPDATA_DATA);

            Map<String,GenTableColumn> columnMap=new HashMap<>();
            Iterator<GenTableColumn> columnListIterator=genTable.getColumns().iterator();
            while(columnListIterator.hasNext()){
                GenTableColumn genTableColumn=columnListIterator.next();
                columnMap.put(genTableColumn.getColumnName(),genTableColumn);
            }

            Map<String, JavaFunction> functionMap=new HashMap<>();
            Map<String,String> othersMap=new HashMap<>();
            LinkedHashMap<String,MappedType> mappedTypeMap=JdbcTypeBiz.getUpdateParamsTypeByDB(columns,datas,columnMap,functionMap,othersMap);
            List<Map<String,Object>> dataList= JdbcTypeBiz.getUpdateDataByDB(datas,dateformat,mappedTypeMap,functionMap);
            response.set(QueryUtil.QUERY_PARAMS,new ArrayList<>(dataList));
            response.set(QueryUtil.QUERY_PARAMS+"_types",new HashMap<>(mappedTypeMap));
            JdbcTypeBiz.filterColumns(columnMap,mappedTypeMap);

            if(isbatch){
                Response res= JdbcTemplateUtils.insertMultiDataBatch(tableName,jdbcTemplate, dataList,mappedTypeMap,othersMap);
                response.setMapValues(res.getMapValues());
            }else{
                Response res=JdbcTemplateUtils.insertMultiData(tableName,jdbcTemplate,dataList,mappedTypeMap,othersMap);
                response.setMapValues(res.getMapValues());
            }
        } catch (Exception e) {
            response.setErr(e);
        }
        return response;
    }
}
